package com.tea.conf;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaQrcodeServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.tea.common.config.WxConf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @PACKAGE_NAME: com.tea.conf
 * @CLASSNAME: WxMaConfiguration
 * @DEACRIPTION:
 * @AUTHOR: GuoChen
 * @DATE: 2024/5/29
 * @WEEK: 周三
 **/
@Configuration
public class WxMaConfiguration {

    @Autowired
    private WxConf wxConf;

    @Bean
    public WxMaService wxMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(wxConf.getAppId());
        config.setSecret(wxConf.getSecret());
        config.setMsgDataFormat(wxConf.getMsgDataFormat());
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }

    /**
     * 注入 二维码相关操作接口.
     *
     * @return
     */
    @Bean
    public WxMaQrcodeService wxMaQrcodeService() {
        WxMaQrcodeService wxMaQrcodeService = new WxMaQrcodeServiceImpl(wxMaService());
        return wxMaQrcodeService;
    }
    
    @Bean
    public WxPayService getWxPayService() {
        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setAppId(wxConf.getAppId());
        wxPayConfig.setMchId(wxConf.getMchId());
        wxPayConfig.setMchKey(wxConf.getMchKey());
        wxPayConfig.setKeyPath(wxConf.getKeyPath());
        wxPayConfig.setTradeType(wxConf.getTradeType());
        wxPayConfig.setNotifyUrl(wxConf.getNotifyUrl());
        
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxPayConfig);
        return wxPayService;
    }
}
